package com.koi.web.controller.demo.easyexecl;

import cn.dev33.satoken.annotation.SaIgnore;
import com.koi.easyexcel.column.ColumnExcelUtil;
import com.koi.easyexcel.column.NoModelWriteData;
import com.koi.easyexcel.column.SimpleWriteData;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * EasyExecl工具类
 *
 * @author ･ᴗ･
 * @description [EasyExecl测试]
 */
@SaIgnore
@RequiredArgsConstructor
@RestController
public class EasyExcelController {

    private final ExcelService excelService;

    /**
     * excel导入-单个sheet
     *
     * @param multipartFile 文件流
     * @return
     * @throws Exception
     */
    @PostMapping("/excelImport")
    public String excelImport(@RequestParam("file") MultipartFile multipartFile) {
        excelService.excelImport(multipartFile);
        return "导入成功";
    }

    /**
     * excel导出-单个sheet[SUCCESS]
     *
     * @param response 响应流
     */
    @GetMapping("/excelExport")
    public void export(HttpServletResponse response) {
        excelService.excelExport(response);
    }

    /**
     * excel导入-多个sheet
     *
     * @param multipartFile 文件流
     * @return 响应体
     */
    @PostMapping("/excelSheetImport")
    public String excelSheetImport(@RequestParam("file") MultipartFile multipartFile) {
        excelService.excelSheetImport(multipartFile);
        return "导入成功";
    }

    /**
     * excel导出-多个sheet[SUCCESS]
     *
     * @param response 响应流
     */
    @GetMapping("/excelSheetExport")
    public void excelSheetExport(HttpServletResponse response) {
        excelService.excelSheetExport(response);
    }

    /**
     * excel模板导出-单个sheet
     *
     * @param response 响应流
     */
    @GetMapping("/excelTemplate")
    public void excelTemplate(HttpServletResponse response) {
        excelService.excelTemplate(response);
    }

    /**
     * excel模板导出-多个sheet[SUCCESS]
     *
     * @param response 响应流
     */
    @GetMapping("/excelSheetTemplate")
    public void excelSheetTemplate(HttpServletResponse response) throws Exception {
        excelService.excelSheetTemplate(response);
    }

    /**
     * 无实体类导出
     *
     * @param response
     * @author ･ᴗ･
     */
    @GetMapping("/excelSheetNoModel")
    public void excelSheetNoModel(HttpServletResponse response) throws Exception {
        // headMap数组定义的是导出文件表头标题的内容，要按顺序定义
        // dataStrMap数据定义的是标题对应的字段名（一定要按顺序对应）
        String[] headMap = {"类型", "房号", "账单单号", "账单生成时间", "计算金额", "优惠金额", "违约金", "应收金额", "录入人", "账单状态"};
        String[] dataStrMap = {"orderClassStr", "houseAddress", "payNo", "createDate", "sumCalAmount", "sumPValue", "sumBreachValue", "sumActualAmount", "creatorName", "payStateStr"};
        PayBillInfoExportBo bo = new PayBillInfoExportBo();
        bo.setCreateDate("2023/02/09");
        bo.setCreatorName("小锦鲤");
        bo.setHouseAddress("郑州市");
        bo.setOrderClassStr("郑州市");
        bo.setPayNo("123456789");
        bo.setSumActualAmount(new BigDecimal("1000"));
        bo.setSumBreachValue(new BigDecimal("1000"));
        bo.setSumCalAmount(new BigDecimal("1000"));
        bo.setSumPValue(new BigDecimal("1000"));
        List<Map<String, Object>> list = new ArrayList<>(); // 这个可以替换为接口数据 List<Map<String, Object>> list = payBillInfoService.listPayBillInfo();
        Map<String, Object> map = new HashMap<>();
        map.put("bo", bo);
        list.add(map);
        NoModelWriteData d = new NoModelWriteData();
        d.setFileName("账单流水");
        d.setHeadMap(headMap);
        d.setDataStrMap(dataStrMap);
        d.setDataList(list);
        ColumnExcelUtil columnExcelUtil = new ColumnExcelUtil();
        columnExcelUtil.noModleWrite(d, response);
    }

    /**
     * 有实体类导出[SUCCESS]
     *
     * @param response
     * @author ･ᴗ･
     */
    @GetMapping("/excelSheetHaveModel")
    public void excelSheetHaveModel(HttpServletResponse response) throws Exception {
        PayBillInfoExportBo bo = new PayBillInfoExportBo();
        bo.setCreateDate("2023/02/09");
        bo.setCreatorName("小小锦鲤");
        bo.setHouseAddress("洛阳");
        bo.setOrderClassStr("郑州市");
        bo.setPayNo("1234567890");
        bo.setSumActualAmount(new BigDecimal("6666"));
        bo.setSumBreachValue(new BigDecimal("6666"));
        bo.setSumCalAmount(new BigDecimal("6666"));
        bo.setSumPValue(new BigDecimal("6666"));
        bo.setPayStateStr("正常");
        List<PayBillInfoExportBo> list = new ArrayList<>();
        list.add(bo);
        SimpleWriteData d = new SimpleWriteData();
        d.setFileName("账单流水");
        d.setDataList(list);
        ColumnExcelUtil columnExcelUtil = new ColumnExcelUtil();
        columnExcelUtil.simpleWrite(d, PayBillInfoExportBo.class, response);
    }


}
